import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

// const Login = () => import('../pages/Login')
// const Layout = () => import('../pages/Layout')
// const Index = () => import('../pages/menu/Index')
// const Role =() => import('../pages/role/Role')
// const Admin =() => import('../pages/admin/Admin')
// const Sort =() => import('../pages/sort/Sort')
// const Specs = () => import('../pages/specs/Specs')
// const Home = () => import('../pages/home/Home')
// const Goods = () => import('../pages/goods/Goods')

import Login from '../pages/Login'
import Layout from '../pages/Layout'
import Index from '../pages/menu/Index'
import Role from '../pages/role/Role'
import Admin from '../pages/admin/Admin'
import Sort from '../pages/sort/Sort'
import Specs from '../pages/specs/Specs'
import Home from '../pages/home/Home'
import Goods from '../pages/goods/Goods'



const Routers = [
  {
    path: "/login",
    name: "login",
    component: Login,
    meta: {
      title: "登陆"
    }
  },
  {
    path: "/",
    name: "layout",
    component: Layout,
    redirect: "/home",
    children:[
      {
        path: "/home",
        name: "home",
        component: Home,
        meta: {
          title: "管理中心"
        }
      },
      {
        path: "/goods",
        name: "goods",
        component: Goods,
        meta: {
          title: "管理中心"
        }
      },
      {
        path: "/specs",
        name: "specs",
        component: Specs,
        meta: {
          title: "商品规格"
        }
      },
      {
        path: "/category",
        name: "sort",
        component: Sort,
        meta: {
          title: "菜单管理"
        }
      },
      {
        path: "/menu",
        name: "menu",
        component: Index,
        meta: {
          title: "菜单管理"
        }
      },
      {
        path: "/role",
        name: "role",
        component: Role,
        meta: {
          title: "角色管理"
        }
      },
      {
        path: "/admin",
        name: "admin",
        component: Admin,
        meta: {
          title: "管理员管理"
        }
      }
    ],
    meta: {
      title: "网站首页"
    }
  }
]

const router = new Router({
  routes: Routers
})

router.beforeEach(
  function(to,from,next){
      //登录问题
      if(to.path != "/login"){
          const userData = JSON.parse(localStorage.getItem("userData"))||{};
          if(!userData.token){
            return next("/login")
          }
      }
      //访问权限
      const url_list = JSON.parse(localStorage.getItem("userData")).menus_url
      url_list.push('/home')
      if(url_list.indexOf(to.path) == -1){
          return next("/home")
      }
      //权限问题
      //获取用户有权限访问的页面
      
      //判断请求地址在不在menus_url当中
      document.title = to.meta.title
      next()
  }
)




export default router
